# coding:utf-8
from PyQt6.QtCore import QSize
from PyQt6.QtWidgets import QApplication, QWidget
from qfluentwidgets import FluentWindow, FluentIcon as FIF, NavigationItemPosition, SplashScreen, MessageBox

from common import SingleIcon, APP_NAME, cfg, signalBus, Icon, OfficeIcon
from components import SlidingStackedWidget
from .excel_interface import ExcelInterface
from .home_interface import HomeInterface
from .image_interface import ImageInterface
from .other_interface import OtherInterface
from .pdf_interface import PDFInterface
from .setting_interface import SettingInterface
from .video_interface import VideoInterface
from .word_interface import WordInterface


class MainWindow(FluentWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.homeInterface = HomeInterface(self)
        self.imageInterface = ImageInterface(self)
        self.videoInterface = VideoInterface(self)
        self.excelInterface = ExcelInterface(self)
        self.wordInterface = WordInterface(self)
        self.pdfInterface = PDFInterface(self)
        self.otherInterface = OtherInterface(self)

        self.settingInterface = SettingInterface(self)

        self.__initWidgets()
        self.__initNavigation()
        self.__connectSignals()
        self.splashScreen.finish()

    def __initWidgets(self):
        self.setMinimumSize(800, 600)
        self.resize(1240, 800)
        self.setWindowIcon(SingleIcon.LOGO.icon())
        self.setWindowTitle(APP_NAME)

        self.navigationInterface.setEnabled(True)
        self.navigationInterface.setExpandWidth(200)

        # create splash screen
        self.splashScreen = SplashScreen(self.windowIcon(), self)
        self.splashScreen.setIconSize(QSize(106, 106))

        geometry = QApplication.primaryScreen().geometry()
        self.move(geometry.width() // 2 - self.width() // 2, geometry.height() // 2 - self.height() // 2)
        self.splashScreen.raise_()
        self.show()
        QApplication.processEvents()

    def __connectSignals(self):
        self.setMicaEffectEnabled(cfg.get(cfg.micaEnabled))
        signalBus.micaEnableChanged.connect(self.setMicaEffectEnabled)
        signalBus.switchToSampleCard.connect(self.switchToSampleCard)
        signalBus.appErrorSig.connect(self.onAppError)

    def __initNavigation(self):
        self.addSubInterface(self.homeInterface, Icon.HOME, '主页')
        self.addSubInterface(self.imageInterface, OfficeIcon.PICTURE, '图片')
        self.addSubInterface(self.videoInterface, SingleIcon.VIDEO, '视频')
        self.addSubInterface(self.excelInterface, OfficeIcon.EXCEL5, 'Excel')
        self.addSubInterface(self.wordInterface, OfficeIcon.WORD, 'Word')
        self.addSubInterface(self.pdfInterface, OfficeIcon.PDF, 'PDF')
        self.addSubInterface(self.otherInterface, SingleIcon.OTHER, '其他')

        position = NavigationItemPosition.BOTTOM
        self.addSubInterface(self.settingInterface, FIF.SETTING, '设置', position=position)

    def resizeEvent(self, e):
        super().resizeEvent(e)
        if hasattr(self, 'splashScreen'):
            self.splashScreen.resize(self.size())

    def switchToSampleCard(self, p_obj: str, obj: str):
        widget: SlidingStackedWidget = self.findChild(QWidget, p_obj)
        if not widget:
            return
        self.stackedWidget.setCurrentWidget(widget, False)
        sub_widget: QWidget = widget.findChild(QWidget, obj)
        if not sub_widget:
            return
        widget.setCurrentWidget(sub_widget)

    def onAppError(self, message: str):
        """ app error slot """
        QApplication.clipboard().setText(message)
        self.showMessageBox(
            self.tr("发生未处理的异常"),
            self.tr("错误消息已写入粘贴板和日志。您要反馈错误信息吗？"),
            True
        )

    def showMessageBox(self, title: str, content: str, showYesButton=False, yesSlot=None):
        """ show message box """
        w = MessageBox(title, content, self)
        if not showYesButton:
            w.cancelButton.setText(self.tr('Close'))
            w.yesButton.hide()
            w.buttonLayout.insertStretch(0, 1)

        if w.exec() and yesSlot is not None:
            yesSlot()
